﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HitachiLib.Data;
using HitachiLib.Utils;
using HitachiLib.HitachiServiceRef;

namespace HitachiApp.Model
{
    public class ProjectSelectorControlModel : INotifyPropertyChanged
    {
        private List<Project> zoon_list = new List<Project>();
        private List<Project> server_list = new List<Project>();
        private List<Project> line_list = new List<Project>();
        private Project zoon = null;
        private Project server = null;
        private Project line = null;
        private Project current = null;
        private ScheduleTabViewModel parent;

        public ProjectSelectorControlModel(ScheduleTabViewModel m)
        {
            parent = m;
        }

        public void SetLoginAccount(Resource r)
        {
            HitachiServiceClient client = WCFClient.GetClient();
            client.GetProjectsByResourceIdCompleted +=
                new EventHandler<GetProjectsByResourceIdCompletedEventArgs>(client_GetProjectsByResourceIdCompleted);
            WCFClient.Retain();
            client.GetProjectsByResourceIdAsync(r.Data.ID);
        }

        private void client_GetProjectsByResourceIdCompleted(object sender, GetProjectsByResourceIdCompletedEventArgs e)
        {
            WCFClient.Release();
            ObservableCollection<ProjectData> pdlist = e.Result;
            List<Project> all = Project.Link(pdlist);
            zoon_list.Clear();
            foreach (Project p in all)
            {
                if (p.Data.Level == 30)
                {
                    zoon_list.Add(p);
                }
            }
            RaisePropertyChanged("ZoonList");
            RaisePropertyChanged("Loaded");
        }

        private void client_GetProjectsByParentIdCompleted(object sender, GetProjectsByParentIdCompletedEventArgs e)
        {
            ObservableCollection<ProjectData> pdlist = e.Result;
            List<Project> all = Project.Link(pdlist);
            parent.SetCurrentProject(current, null);
        }

        private void client_GetWholeLineDataCompleted(object sender, GetWholeLineDataCompletedEventArgs e)
        {
            WCFClient.Release();
            WholeLineData data = e.Result;
            parent.SetCurrentProject(current, data);
        }

        public bool Loaded
        {
            get { return zoon_list.Count > 0; }
        }

        public List<Project> ZoonList
        {
            get { return zoon_list; }
        }
        public Project Zoon
        {
            get { return zoon; }
            set
            {
                if (!(value is Project))
                    return;
                zoon = value;
                Project selected = value as Project;
                server_list.Clear();
                line_list.Clear();
                foreach (Project c in selected.Children.Values)
                {
                    if (c != null && c.Data.ID > 0)
                    {
                        server_list.Add(c);
                    }
                }
                RaisePropertyChanged("ServerList");
                RaisePropertyChanged("LineList");
                current = value;
            }
        }
        public List<Project> ServerList
        {
            get { return server_list; }
        }
        public Project Server
        {
            get { return server; }
            set
            {
                if (!(value is Project))
                    return;
                server = value;
                Project selected = value as Project;
                line_list.Clear();
                foreach (Project c in selected.Children.Values)
                {
                    if (c != null && c.Data.ID > 0)
                    {
                        line_list.Add(c);
                    }
                }
                RaisePropertyChanged("LineList");
                current = value;
            }
        }
        public List<Project> LineList
        {
            get { return line_list; }
        }
        public Project Line
        {
            get { return line; }
            set
            {
                if (!(value is Project))
                    return;
                line = value;
                Project selected = value as Project;
                current = value;

                parent.SetCurrentProject(null, null);

                HitachiServiceClient client = WCFClient.GetClient();
//                client.GetProjectsByParentIdCompleted +=
//                    new EventHandler<GetProjectsByParentIdCompletedEventArgs>(client_GetProjectsByParentIdCompleted);
//                client.GetProjectsByParentIdAsync(value.Id);
                client.GetWholeLineDataCompleted +=
                    new EventHandler<GetWholeLineDataCompletedEventArgs>(client_GetWholeLineDataCompleted);
                WCFClient.Retain();
                client.GetWholeLineDataAsync(value.Id);
            }
        }

        #region PropertChanged Block
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion
    }
}
